<?php

namespace app\api\model;
use think\Model;
use think\facade\Db;

class Base extends Model {

  public $fields = [];
  public $searchFields = [];

  public function getFields() {
    $prefix = env('database.prefix');
    $table = $prefix . strtolower($this->name);
    $result = Db::query('show full columns from '.$table);
    foreach($result as $item) {
      $field = $item['Field'];
      $this->fields[] = $field;
    }
    return $this->fields;
  }

  public function getAll() {
    return $this->select()->toArray();
  }

  public function get($query) {

    $fields = implode('|', $this->searchFields);
    $key = $query['key'];
    return $this->where($fields, 'like', "%$key%")
                ->order($query['order'])
                ->page($query['page'], $query['limit'])
                ->select()
                ->toArray();

  }

  public function getById($id) {
    return $this->find($id)->toArray();
  }

  public function deleteById($id) {
 
    $result = $this->whereIn('id', $id);
    return $result->delete();

  }

} 